﻿/*
* Infrastructure 参数信息
*/

//定义js命名空间
Ext.namespace("Infrastructure.Params");

// 所有Statestore
Infrastructure.Params.StateStore = new Ext.data.ArrayStore({
	id: 0,
	fields: [
		'Code',
		'Name'
	],
	data: [['0', '停用'], ['1', '正常']]
});

// 审批状态
Infrastructure.Params.ApprStateStore = new Ext.data.ArrayStore({
	id: 0,
	fields: [
		'Code',
		'Name'
	],
	data: [['0', '未审批'], ['-1', '否决'], ['1', '同意']]
});

// 审批状态
Infrastructure.Params.ApprTypeStore = new Ext.data.ArrayStore({
	id: 0,
	fields: [
		'Code',
		'Name'
	],
	data: [[0, '公务用车'], [1, '商品车调拨'], [2, '商品车派车']]
});

// 下拉框
Infrastructure.Params.InfraLocalComboBox = Ext.extend(Ext.form.ComboBox, {
	store: null,
	filterKey: '',
	filterValue: '',
	queryField: 'Code|Name|InputCode1|InputCode2|InputCode3', //输入是查询field
	valueField: 'Code',
	codeField:'Code',
	displayField: 'Name',
	emptyText: "请选择...",
	forceSelection: true,
	allowBlank: false,
	triggerAction: 'all',
	mode: 'local', //可选值local/remote之一,从本地还是远程取数据
	width: 150,
	listWidth: 200,
	typeAheadDelay: 10000,
	initComponent: function () {
		Ext.applyIf(this, {
			tpl: new Ext.XTemplate(
			'<div class="x-grid3"><div><table>',
			'<thead class="x-grid3-header"><tr class="x-grid3-hd-row">',
			'<td class="x-grid3-hd x-grid3-cell x-grid3-hd-inner" style="width: 100px;color: blue;">编码</td>',
			'<td class="x-grid3-hd x-grid3-cell" style="width: 150px;color: blue;">名称</td>',
			'</tr></thead><tbody>',
			'<tpl for=".">',
			'<tr class="x-combo-list-item">',
			'<td class="x-grid3-col x-grid3-cell"><div class="x-grid3-cell-inner"  style="font-size:12px;">{' + this.codeField + '}</div></td>',
			'<td class="x-grid3-col x-grid3-cell"><div class="x-grid3-cell-inner"  style="font-size:12px;">{' + this.displayField + '}</div></td>',
			'</tr>', '</tpl>', '</tbody></table></div></div>')
		});
		Infrastructure.Params.InfraLocalComboBox.superclass.initComponent.call(this);
	},
	listeners: {
		'expand': function (thiz) {
			if (this.filterKey != '')
				thiz.store.filter(this.filterKey, this.filterValue);
		},
		'select': function (thiz) {
			thiz.store.clearFilter();
		},
		'beforequery': function (qe) {
			var combo = qe.combo;
			var q = qe.query;
			var forceAll = qe.forceAll;
			if (!qe.forceAll || (q.length >= this.minChars)) {
				if (this.lastQuery !== q) {
					this.lastQuery = q;
					if (this.mode == 'local') {
						this.selectedIndex = -1;
						if (forceAll) {
							this.store.clearFilter();
						} else {
							// 检索的正则
							q.replace(/[^\w\u4e00-\u9fa5]/g, '');
							var regExp = new RegExp(".*" + q + ".*", "i");
							var qFields = this.queryField.split('|');
							var filterfn = function (record, id) {
								// 得到每个record的项目名称值
								var qt = regExp.test(record.get(combo.valueField));
								qt = qt || regExp.test(record.get(combo.displayField));
								for (var i in qFields) {
									qt = qt || regExp.test(record.get(qFields[i]));
								}
								return qt;
							};
							if (this.filterKey != '') {
								this.store.filter([{
									property: this.filterKey,
									value: this.filterValue
								}, {
									fn: filterfn
								}]);
							} else {
								this.store.filter([{
									fn: filterfn
								}]);
							}
						}
						this.onLoad();
					} else {
						this.store.baseParams[this.queryParam] = q;
						this.store.load({
							params: this.getParams(q)
						});
						this.expand();
					}
				} else {
					this.selectedIndex = -1;
					this.onLoad();
				}
			}
			return false;
		}
	}
});
Ext.reg('infra_localcombo', Infrastructure.Params.InfraLocalComboBox);
Ext.reg('infra_localdictcombo', Infrastructure.Params.InfraLocalComboBox);

// 所有部门store
Infrastructure.Params.DeptStore = new Infrastructure.DeptJsonStore({
	autoLoad: true
});

// 返回所有用户
Infrastructure.Params.UserStore = new Infrastructure.UserJsonStore({
	autoLoad: true
});

// 返回当前用户同一部门的用户
Infrastructure.Params.DeptUserStore = new Infrastructure.UserJsonStore({
	url: '/InfraHospUser/GetUserListByDept'
});

// 用户组Store
Infrastructure.Params.TeamGroupStore = new Infrastructure.DictJsonStore({
	autoLoad: true,
	category: 'TeamGroup'
});

// ID卡分类Store
Infrastructure.Params.IDCardTypeStore = new Ext.data.ArrayStore({
	id: 0,
	fields: [
		'Code',
		'Name'
	],
	data: [
		[1, '员工信息卡'],
		[2, '商品车信息卡'],
		[3, '商品车钥匙卡'],
		[4, '公务车辆通行卡'],
		[5, '员工车辆通行卡'],
		[6, '外来车辆通行卡'],
		[7, '会员卡']
	]
});

// ID卡使用情况Store
Infrastructure.Params.IDCardStateStore = new Ext.data.ArrayStore({
	id: 0,
	fields: [
		'Code',
		'Name'
	],
	data: [[0, '未使用'], [1, '已使用']]
});

/*
custom ext
*/
// 部门多选下拉
Infrastructure.Params.DeptartmentSuperBoxSelect = Ext.extend(Ext.ux.form.SuperBoxSelect, {
	allowBlank: false,
	fieldLabel: '所属机构',
	emptyText: '请选择...',
	minChars: 0,
	hiddenName: 'TmpDepartments',
	store: Infrastructure.Params.DeptStore,
	mode: 'remote',
	displayField: 'Name',
	valueField: 'Id',
	forceSelection: true,
	typeAheadDelay: 10000,
	triggerAction: 'all',
	listWidth: 250,
	width: 430,
	pageSize: 10,
	tpl: new Ext.XTemplate(
					'<div class="x-grid3"><div><table>',
					'<thead class="x-grid3-header"><tr class="x-grid3-hd-row">',
					'<td class="x-grid3-hd x-grid3-cell x-grid3-hd-inner" style="width: 50px;color: blue;">编码</td>',
					'<td class="x-grid3-hd x-grid3-cell" style="width: 200px;color: blue;">名称</td>',
					'</tr></thead><tbody>',
					'<tpl for=".">',
					'<tr class="x-combo-list-item">',
					'<td class="x-grid3-col x-grid3-cell"><div class="x-grid3-cell-inner"  style="font-size:12px;">{Code}</div></td>',
					'<td class="x-grid3-col x-grid3-cell"><div class="x-grid3-cell-inner"  style="font-size:12px;">{Name}</div></td>',
					'</tr>', '</tpl>', '</tbody></table></div></div>')
});
Ext.reg('infra_dept_supcombo', Infrastructure.Params.DeptartmentSuperBoxSelect);

// 用户多选下拉
Infrastructure.Params.UserSuperBoxSelect = Ext.extend(Ext.ux.form.SuperBoxSelect, {
	allowBlank: false,
	fieldLabel: '用户',
	emptyText: '请选择...',
	minChars: 0,
	hiddenName: 'TmpUsers',
	store: Infrastructure.Params.UserStore,
	mode: 'remote',
	displayField: 'Name',
	valueField: 'Id',
	forceSelection: true,
	typeAheadDelay: 10000,
	triggerAction: 'all',
	listWidth: 250,
	width: 430,
	pageSize: 10,
	tpl: new Ext.XTemplate(
					'<div class="x-grid3"><div><table>',
					'<thead class="x-grid3-header"><tr class="x-grid3-hd-row">',
					'<td class="x-grid3-hd x-grid3-cell x-grid3-hd-inner" style="width: 50px;color: blue;">编码</td>',
					'<td class="x-grid3-hd x-grid3-cell" style="width: 200px;color: blue;">名称</td>',
					'</tr></thead><tbody>',
					'<tpl for=".">',
					'<tr class="x-combo-list-item">',
					'<td class="x-grid3-col x-grid3-cell"><div class="x-grid3-cell-inner"  style="font-size:12px;">{Code}</div></td>',
					'<td class="x-grid3-col x-grid3-cell"><div class="x-grid3-cell-inner"  style="font-size:12px;">{Name}</div></td>',
					'</tr>', '</tpl>', '</tbody></table></div></div>')
});
Ext.reg('infra_user_supcombo', Infrastructure.Params.UserSuperBoxSelect);


// ID卡分类Store
Infrastructure.Params.TestStore = new Ext.data.ArrayStore({
	id: 0,
	fields: [
		'Code',
		'Dict',
		'Name'
	],
	data: [
		[1, 'dict2', '不应该这个'],
		[1, 'dict1', '员工信息卡-对'],
		[2, 'dict1', '商品车信息卡'],
		[3, 'dict1', '商品车钥匙卡'],
		[4, 'dict1', '公务车辆通行卡'],
		[5, 'dict2', '员工车辆通行卡'],
		[6, 'dict2', '外来车辆通行卡'],
		[7, 'dict2', '会员卡']
	]
});

//Infra常用字典Store
Infrastructure.Params.DictionaryStore = new Ext.data.JsonStore({
	url: '/InfraDictionary/GetPlistAll',
	storeId: 'Infra_DictionaryStore',
	idProperty: 'Code',
	root: 'data',
	fields: [{ name: 'TreeCode', mapping: 'Category.TreeCode' }, 'Id', 'Name', 'Code', 'InputCode1', 'InputCode2', 'InputCode3']
});

Infrastructure.Params.DictionaryStore.load();

Infrastructure.Params.DictionaryRender = function (v) {
	return v == null ? '' : (Infrastructure.Params.DictionaryStore.getById(v) == undefined ? '' : Infrastructure.Params.DictionaryStore.getById(v).get('Name'));
};

// 字典类
Infrastructure.Params.InfraDictComboBox = Ext.extend(Infrastructure.Params.InfraLocalComboBox, {
	filterKey: 'TreeCode',
	queryField: 'Code|Name|InputCode1|InputCode2|InputCode3', //输入是查询field
	store: Infrastructure.Params.DictionaryStore
});
Ext.reg('infra_dictcombo', Infrastructure.Params.InfraDictComboBox);

Infrastructure.Params.DictionaryColumn = Ext.extend(Ext.grid.Column, {
	dictCode: this.dataIndex,
	renderer: Infrastructure.Params.DictionaryRender,
	constructor: function (cfg) {
		cfg.editor = new Infrastructure.Params.InfraDictComboBox({
			filterValue: cfg.dictCode
		}),
		Infrastructure.Params.DictionaryColumn.superclass.constructor.call(this, cfg);
	}
});
Ext.grid.Column.types.infra_dictcolumn = Infrastructure.Params.DictionaryColumn;

//获取排序List类型的array
//Example : Infrastructure.Params.GetDictFilterListData(Infrastructure.Params.DictionaryStore, 'TreeCode', 'Inout', 'Code', 'Name')
Infrastructure.Params.GetDictFilterListData = function (store, dictField, dictValue, idField, textField) {
	if (dictField != '')
		store.filter(dictField, dictValue);
	var r = store.data.items;
	var resultArray = [];
	if (r) {
		var rlen = r.length;
		for (var i = 0; i < rlen; i++) {
			resultArray.push({
				id: r[i].get(idField),
				text: r[i].get(textField)
			});
		}
	}
	store.clearFilter();
	return resultArray;
};

Infrastructure.Params.DictFilter = function (dict) {
	return Infrastructure.Params.GetDictFilterListData(Infrastructure.Params.DictionaryStore, 'TreeCode', dict, 'Code', 'Name');
}

/*******************************************
* Store列
*******************************************/
Infrastructure.Params.KeyValueFilter = function (store) {
	return Infrastructure.Params.GetDictFilterListData(store, '', '', 'Value', 'Key');
}

//字典Store列
Infrastructure.Params.KeyValueColumn = Ext.extend(Ext.grid.Column, {
	dataStore: null,
	dataValue: 'Key',
	constructor: function (cfg) {
		cfg = cfg || {};
		Infrastructure.Params.KeyValueColumn.superclass.constructor.call(this, Ext.apply({
			renderer: function (v) {
				return v == null ? '' : this.dataStore == undefined ? 'undefined store' : (this.dataStore.getById(v) == undefined ? '' : this.dataStore.getById(v).get(this.dataValue));
			}
		}, cfg));
	}
});
Ext.grid.Column.types.infra_KeyValueColumn = Infrastructure.Params.KeyValueColumn;

//字典 Key-Value下拉
Infrastructure.Params.KeyValueCombobox = Ext.extend(Infrastructure.Params.InfraLocalComboBox, {
	queryField: 'Key|Value', //输入是查询field
	valueField: 'Value',
	displayField: 'Key'
});
Ext.reg('infra_KeyValueCombo', Infrastructure.Params.KeyValueCombobox);


/*******************************************
* 机构部门
*******************************************/

//机构部门清单Store
Infrastructure.Params.DeptStore = new Ext.data.JsonStore({
	idProperty: 'Id',
	root: 'data',
	url: '/InfraHospUser/GetDeptList',
	autoLoad: true,
	fields: ['Id', 'Name', 'Code', 'InputCode1', 'InputCode2', 'InputCode3']
});

Infrastructure.Params.DepartmentRender = function (v) {
	return v == null ? '' : (Infrastructure.Params.DeptStore.getById(v) == undefined ? '' : Infrastructure.Params.DeptStore.getById(v).get('Name'));
};

Infrastructure.Params.DepartmentFilter = function () {
	return Infrastructure.Params.GetDictFilterListData(Infrastructure.Params.DeptStore, '', '', 'Id', 'Name');
}

//机构部门列
Infrastructure.Params.DepartmentColumn = Ext.extend(Ext.grid.Column, {
	renderer: Infrastructure.Params.DepartmentRender,
	constructor: function (cfg) {
		Infrastructure.Params.DepartmentColumn.superclass.constructor.call(this, cfg);
	}
});
Ext.grid.Column.types.infra_deptcolumn = Infrastructure.Params.DepartmentColumn;

//机构下拉
Infrastructure.Params.DepartmentCombobox = Ext.extend(Infrastructure.Params.InfraLocalComboBox, {
	fieldLabel: "展场",
	filterKey: '',
	valueField: 'Id',
	queryField: 'Code|Name|InputCode1|InputCode2|InputCode3', //输入是查询field
	store: Infrastructure.Params.DeptStore
});
Ext.reg('infra_deptcombo', Infrastructure.Params.DepartmentCombobox);


/*******************************************
* 用户部分
*******************************************/
Infrastructure.Params.InfraUserStore = new Ext.data.JsonStore({
	idProperty: 'Id',
	root: 'data',
	url: '/InfraHospUser/GetPlist',
	autoLoad: true,
	fields: ['Id', 'Name', 'Code', 'InputCode1', 'InputCode2', 'InputCode3']
});

Infrastructure.Params.InfraUserRender = function (v) {
	return v == null ? '' : (Infrastructure.Params.InfraUserStore.getById(v) == undefined ? v : Infrastructure.Params.InfraUserStore.getById(v).get('Name'));
};

Infrastructure.Params.InfraUserFilter = function () {
	return Infrastructure.Params.GetDictFilterListData(Infrastructure.Params.InfraUserStore, '', '', 'Id', 'Name');
}

//用户列
Infrastructure.Params.InfraUserColumn = Ext.extend(Ext.grid.Column, {
	renderer: Infrastructure.Params.InfraUserRender,
	constructor: function (cfg) {
		Infrastructure.Params.InfraUserColumn.superclass.constructor.call(this, cfg);
	}
});
Ext.grid.Column.types.infra_usercolumn = Infrastructure.Params.InfraUserColumn;

//用户下拉
Infrastructure.Params.InfraUserCombobox = Ext.extend(Infrastructure.Params.InfraLocalComboBox, {
	fieldLabel: '用户',
	filterKey: '',
	valueField: 'Id',
	queryField: 'Code|Name|InputCode1|InputCode2|InputCode3', //输入是查询field
	store: Infrastructure.Params.InfraUserStore
});
Ext.reg('infra_usercombo', Infrastructure.Params.InfraUserCombobox);

//用户下拉
Infrastructure.Params.InfraUserByDeptCombobox = Ext.extend(Infrastructure.Params.InfraLocalComboBox, {
	fieldLabel: '用户',
	filterKey: '',
	valueField: 'Id',
	queryField: 'Code|Name|InputCode1|InputCode2|InputCode3', //输入是查询field
	store: Infrastructure.Params.InfraUserStore
});
Ext.reg('infra_userbydeptcombo', Infrastructure.Params.InfraUserByDeptCombobox);
